package com.bt.framework.utils

import android.annotation.SuppressLint
import android.app.Activity
import android.graphics.Color
import android.graphics.Outline
import android.os.Build
import android.view.View
import android.view.ViewOutlineProvider
import android.view.WindowManager

/**
 *@author:hxm
 *@date: 2025/6/22 15:45
 *@version:
 *@description:View视图处理工具类
 */
object ViewUtil {

    /**
     * 设置View圆角
     * @param view View? 需要设置圆角的View
     * @param radius Int 圆角半径
     */
    fun setClipViewCornerRadius(view: View?, radius: Int) {
        view?.run {
            clipToOutline = radius > 0
            if (clipToOutline) {
                outlineProvider = object : ViewOutlineProvider() {
                    override fun getOutline(view: View?, outline: Outline?) {
                        outline?.setRoundRect(
                            0,
                            0,
                            view?.width ?: 0,
                            view?.height ?: 0,
                            radius.toFloat()
                        )
                    }
                }
            }
        }
    }

    /**
     * 设置View顶部圆角
     * @param view View? 需要设置圆角的View
     * @param radius Int 圆角半径
     */
    fun setClipViewCornerTopRadius(view: View?, radius: Int) {
        view?.run {
            clipToOutline = radius > 0
            if (clipToOutline) {
                outlineProvider = object : ViewOutlineProvider() {
                    override fun getOutline(
                        view: View?,
                        outline: Outline?
                    ) {
                        outline?.setRoundRect(
                            0,
                            0,
                            view?.width ?: 0,
                            (view?.height ?: 0) + radius,
                            radius.toFloat()
                        )
                    }

                }
            }
        }
    }

    /**
     * 底部圆角
     * @param view View? 需要设置圆角的View
     * @param radius Int 圆角半径
     */
    fun setClipViewCornerBottomRadius(view: View?, radius: Int) {
        view?.run {
            clipToOutline = radius > 0
            if (clipToOutline) {
                outlineProvider = object : ViewOutlineProvider() {
                    override fun getOutline(
                        view: View?,
                        outline: Outline?
                    ) {
                        outline?.setRoundRect(
                            0,
                            -radius,
                            view?.width ?: 0,
                            view?.height ?: 0,
                            radius.toFloat()
                        )
                    }

                }
            }
        }
    }

    /**
     * 透明状态栏
     * @param activity Activity
     * @param isDarkText Boolean 是否深色字体
     */
    @SuppressLint("ObsoleteSdkInt")
    fun transparentBar(activity : Activity, isDarkText : Boolean = false){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
           val window = activity.window
            window?.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            window?.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window?.statusBarColor = Color.TRANSPARENT
            var systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            if (isDarkText && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                systemUiVisibility = systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            }
            window?.decorView?.systemUiVisibility = systemUiVisibility
        }else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
            val window = activity.window
            window?.setFlags(
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
            )
        }
    }

}